package com.sharp.authorization.dao.common.repository;

import com.sharp.authorization.dao.common.entity.AcUserRole;
import com.sharp.authorization.dao.common.mapper.AcUserRoleDao;
import com.sharp.kernel.mybatis.repository.BaseMysqlRepository;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Title: AcUserRoleService
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2022/6/16 14:20
 */
@Service
public class AcUserRoleService extends BaseMysqlRepository<AcUserRole, AcUserRoleDao> {

    @Resource
    private AcUserRoleDao acUserRoleDao;

    public List<AcUserRole> getUserRoleByRoleId(String roleId) {
        Example example = buildExample(roleId, null);
        return select(example);
    }

    private Example buildExample(String roleId, String userId) {
        Example example = super.getExample();
        Example.Criteria criteria = example.createCriteria();
        if (roleId != null) {
            criteria.andEqualTo("roleId", roleId);
        }
        if (userId != null) {
            criteria.andEqualTo("userId", userId);
        }
        return example;
    }

    public List<AcUserRole> getUserRoleListByRoleId(String roleId) {
        return getUserRoleListByRoleIdAndUserId(roleId, null);
    }

    public List<AcUserRole> getUserRoleListByRoleIdAndUserId(String roleId, String userId) {
        Example example = buildExample(roleId, userId);
        return super.select(example);
    }

    public AcUserRole getUserRoleByUserId(String userId) {
        return getUserRoleByRoleIdAndUserId(null, userId);
    }

    public AcUserRole getUserRoleByRoleIdAndUserId(String roleId, String userId) {
        Example example = buildExample(roleId, userId);
        return super.selectOne(example);
    }

    public List<AcUserRole> getUserRoleListByUserId(String userId) {
        return getUserRoleListByRoleIdAndUserId(null, userId);
    }

    public void deleteByUserId(String userId) {
        Example example = buildExample(null, userId);
        super.delete(example);
    }

    public void bind(String userId, List<String> roleList) {
        List<AcUserRole> list = new ArrayList<>();
        for (String roleId : roleList) {
            AcUserRole userRole = buildUserRole(userId, roleId);
            list.add(userRole);
        }
        super.insertBatch(list);
    }

    private AcUserRole buildUserRole(String userId, String roleId) {
        AcUserRole userRole = new AcUserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        return userRole;
    }

    public void bind(String userId, String roleId) {
        AcUserRole userRole = buildUserRole(userId, roleId);
        super.insert(userRole);
    }

    public void replaceBind(String userId, String roleId) {
        Example example = buildExample(null, userId);
        super.delete(example);
        AcUserRole userRole = buildUserRole(userId, roleId);
        super.insertOrUpdateSelective(userRole);
    }
}
